<template>
  <div class="app-container">
    <el-page-header content="我的发布" @back="goBack" />
    <el-divider />
    <div style="width: 20vw;">
      <el-input v-model="params.search" placeholder="请输入关键字" />
      <el-button type="primary" icon="el-icon-search" @click="getData">搜索</el-button>
    </div>
    <el-divider />
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column label="编号">
        <template v-slot="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template v-slot="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="型号">
        <template v-slot="scope">
          <span>{{ scope.row.model }}</span>
        </template>
      </el-table-column>
      <el-table-column label="描述">
        <template v-slot="scope">
          <span>{{ scope.row.description }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格（元）">
        <template v-slot="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折旧程度">
        <template v-slot="scope">
          <span>{{ scope.row.oldLevel }}</span>
        </template>
      </el-table-column>
      <el-table-column label="购买时间">
        <template v-slot="scope">
          <span>{{ scope.row.buyTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template v-slot="scope">
          <el-img :src="'http://localhost:8082/api/comm/'+scope.row.imageList[0]" alt="商品图片" fit="scale-down" width="100" height="100">
            <div slot="error" class="image-slot">
              <el-empty description="暂无图片" image-size="50" />
            </div>
          </el-img>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template v-slot="scope">
          <el-tag :type="scope.row.status==='在售'?'':'danger'">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请状态">
        <template v-slot="scope">
          <el-tag :type="scope.row.flow==='通过'?'':'danger'">{{ scope.row.flow }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="分类">
        <template v-slot="scope">
          <span>{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column label="卖家编号">
        <template v-slot="scope">
          <span>{{ scope.row.sellerId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览次数">
        <template v-slot="scope">
          <span>{{ scope.row.viewCount }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template v-slot="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="150"
      >
        <template v-slot="scope">
          <el-popconfirm v-if="scope.row.status==='在售'" title="确认卖出吗？" @confirm="confirmSales(scope.row)">
            <el-button slot="reference" size="small">确认卖出</el-button>
          </el-popconfirm>
          <el-popconfirm title="确认编辑吗？" @confirm="openEdit(scope.row)">
            <el-button slot="reference" size="small">编辑</el-button>
          </el-popconfirm>
          <el-popconfirm title="确认删除吗？" icon-color="red" @confirm="handleDelete(scope.row)">
            <el-button slot="reference" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-divider />
  </div>
</template>

<script>

import { delComm, getMyListComm, urlGetComm, urlPostComm } from '@/api/comm'
import { successMsg } from '@/utils/comm'

export default {
  data() {
    return {
      module: 'product',
      list: null,
      listLoading: true,
      params: {}
    }
  },
  created() {
    this.getData()
  },
  methods: {
    confirmSales(row) {
      this.$prompt('请输入卖家', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        urlPostComm(this.module + '/confirmSales/' + value, row).then(rs => {
          successMsg()
          this.getData()
        })
      })
    },
    openEdit(row) {
      this.$router.push('/admin/product/edit/' + row.id)
    },
    handleDelete(row) {
      delComm(this.module, row.id).then(response => {
        successMsg()
        this.getData()
      })
    },
    openAdd() {
      this.$router.push('/admin/product/add')
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.params.pageNum = val
      this.getData()
    },
    getData() {
      this.listLoading = true
      getMyListComm(this.module, this.params).then(response => {
        this.list = response.data
        this.listLoading = false
      })
    },
    goBack() {
      this.$router.push('/dashboard')
    }
  }
}
</script>
<style>
.el-select {
  width: 5vw;
}

</style>

